package uml.GUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EnumSet;

import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

import uml.eventsystem.IObserver;
import uml.eventsystem.ISubject;
import uml.eventsystem.SubjectImpl;
import uml.eventsystem.events.UMLEvent;
import uml.eventsystem.events.UMLEventId;
import uml.objects.UMLObject;
import uml.view.factory.UMLObjectFactory;

public class ControlPalatte
		extends JPanel
		implements ActionListener, ISubject, IObserver
{
	EnumSet<UMLEventId> registeredEvents = EnumSet.allOf (UMLEventId.class);
	SubjectImpl subjectImplementation = new SubjectImpl (this);

	/**
	 * Creates a default <tt>ControlPalatte</tt>
	 * 
	 * @param controller
	 */
	
	public ControlPalatte (IObserver observer)
	{
		this.setLayout (new MigLayout ("wrap 1"));
		addButton (UMLEventId.MAKE_CLASS);
		addButton (UMLEventId.MAKE_NOTE);
		addButton (UMLEventId.MAKE_PACKAGE);
		addButton (UMLEventId.MAKE_INTERFACE);
		addButton (UMLEventId.MAKE_ABSTRACT);
		registerForEvents (observer);
	}

	private void addButton (UMLEventId eventId)
	{
		if (eventId.is (UMLEventId.FACTORY))
		{
			UMLObject umlObject = UMLObjectFactory.createUMLObject (eventId);
			if (umlObject.getComponent ().getPreferredSize ().getHeight () >= 100)
				umlObject.getComponent ().setPreferredSize (new Dimension (100, 100));
			UMLObjectButton button = new UMLObjectButton (umlObject.getComponent (), eventId);
			button.addActionListener (this);
			button.addObserver (this, eventId);
			registeredEvents.add (eventId);
			add (button);
		}
	}

	/**
	 * Handles adding a button to the ControlPalatte that displays the text of
	 * name and sends out the eventID event
	 * 
	 * @param name
	 * @param event
	 */
	
	private void addButton (String name, UMLEventId event)
	{
		UMLObjectButton button = new UMLObjectButton (name, event);
		button.addActionListener (this);
		registeredEvents.add (event);
		add (button);
	}

	/**
	 * registers observer for all events that this object has
	 * 
	 * @param observer
	 */
	x
	private void registerForEvents (IObserver observer)
	{
		for (UMLEventId event : registeredEvents)
			addObserver (observer, event);
	}

	@Override
	public void addObserver (IObserver observer, UMLEventId eventId)
	{
		subjectImplementation.addObserver (observer, eventId);

	}

	@Override
	public void removeObserver (IObserver observer, UMLEventId eventId)
	{
		subjectImplementation.removeObserver (observer, eventId);
	}

	@Override
	public void notifyObservers (UMLEventId eventId)
	{
		subjectImplementation.notifyObservers (eventId);
	}

	@Override
	public void actionPerformed (ActionEvent action)
	{
		UMLObjectButton button = (UMLObjectButton) action.getSource ();
		notifyObservers (button.getUMLEventId ());
	}

	@Override
	public void handleNotification (UMLEvent event)
	{
		notifyObservers (event.getEventId ());
	}
}